/**
 * @projectName: project02bywdmtz
 * @package: com.cskaoyan.service
 * @className: StatServiceImpl
 * @author: 冉夏
 * @description: TODO
 * @date: 2022/12/30 9:06
 * @version: 1.8
 */

package com.cskaoyan.service;

import com.cskaoyan.bean.MarketUser;
import com.cskaoyan.bean.MarketUserExample;
import com.cskaoyan.bean.bo.StatGoodBo;
import com.cskaoyan.bean.bo.StatOrderBo;
import com.cskaoyan.bean.bo.StatUserRowsBo;
import com.cskaoyan.bean.vo.StatGoodsVo;
import com.cskaoyan.bean.vo.StatOrderVo;
import com.cskaoyan.bean.vo.StatUserVo;
import com.cskaoyan.mapper.MarketOrderGoodsMapper;
import com.cskaoyan.mapper.MarketOrderMapper;
import com.cskaoyan.mapper.MarketUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.Delayed;
import java.util.stream.Stream;

@Service
public class StatServiceImpl implements StatService{
    @Autowired
    MarketUserMapper marketUserMapper;

/*
* 统计用户在固定时间的注册数量
* */
    @Override
    public StatUserVo statUserNumByRegisTime() {


        //获得按照时间顺序排列的对象
        List<StatUserRowsBo> statUserRowsBo2=marketUserMapper.selectUserStatus();
        System.out.println(statUserRowsBo2);
        //List<MarketUser> marketUsers = marketUserMapper.selectByExample(marketUserExample);
        // MarketUserExample marketUserExample = new MarketUserExample();
        //获得所有的数据库中的所有user
        /*

        LinkedList<StatUserRowsBo> statUserRowsBos = new LinkedList<>();
        int usersNum=1;
        HashMap<Date, Integer> dateIntegerHashMap = new HashMap<>();

        for (MarketUser marketUser : marketUsers) {
            //获得当前的对象的时间,且放入到时间的集合中
            Date addTime = marketUser.getAddTime();
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat();
            String format = simpleDateFormat.format(addTime);
            System.out.println(format);
            int index = format.indexOf(" ");
            //System.out.println(index);
            String substring = format.substring(0, index);
            System.out.println(substring);
            SimpleDateFormat s=new SimpleDateFormat("yy-MM-dd");
            try {
                Date parse = s.parse(format);
                //System.out.println(parse);

                StatUserRowsBo statUserRowsBo1 = new StatUserRowsBo();
                //创建一个集合 内部放的是所有的时间 且不重复
                //LinkedList<Date> addTimeList = new LinkedList<>();
                if(dateIntegerHashMap.containsKey(parse)){
                    //如果包含的话,计数器加加,且更新key的值
                    usersNum++;
                    //更改当前key对应的value值
                    dateIntegerHashMap.put(parse,usersNum);
                }else {
                    //如果不包含的话 放入map中
                    dateIntegerHashMap.put(parse,1);
                }
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        //循环结束后,会得到一个不重复的map,且包含正确的count值,将这个map的值放入需要返回的对象中
        for (Map.Entry<Date, Integer> dateIntegerEntry : dateIntegerHashMap.entrySet()) {
            StatUserRowsBo statUserRowsBo = new StatUserRowsBo();
            statUserRowsBo.setDay(dateIntegerEntry.getKey());
            statUserRowsBo.setUsers(dateIntegerEntry.getValue());
            //将当前含有时间和个数的对象加入到集合中去
            statUserRowsBos.add(statUserRowsBo);
        }

        */
        StatUserVo statUserVo = new StatUserVo();
        statUserVo.setColumns();
        //statUserVo.setRows(statUserRowsBos);
        statUserVo.setRows(statUserRowsBo2);
        return statUserVo;

    }
    /*
    * 按照日期统计每日订单的总额,订单数量,平均值,用户量
    * */
    @Autowired
    MarketOrderMapper orderMapper;
    @Override
    public StatOrderVo getStatOrderInfo() {
        //查询包含订单总额 数量 平均值 用户量
        List<StatOrderBo> statOrderList =orderMapper.selectOrderByDate();
        StatOrderVo statOrderVo = new StatOrderVo();
        //给需要返回的对象赋值
        statOrderVo.setColumns();
        statOrderVo.setRows(statOrderList);
        return statOrderVo;
    }
/*
* 按照日期 统计所有的商品的信息
* */
    @Autowired
    MarketOrderGoodsMapper goodsMapper;
    @Override
    public StatGoodsVo getStatGoodsInfo() {
        List<StatGoodBo> statGoodBoList=goodsMapper.selectStatGoodsInf();
        StatGoodsVo statGoodsVo = new StatGoodsVo();
        //给需要返回的对象赋值
        statGoodsVo.setColumns();
        statGoodsVo.setRows(statGoodBoList);
        return statGoodsVo;
    }
}
